<template>
  <div>
    <tab-helper :paneList="paneList" @tabIndex="tabIndex">
      <div slot="1" class="my-emissionBasis">
        <!-- 基本分类 -->
        <div class="emissionBasis-content">
          <div class="emissionBasis-content-title">基本分类</div>
          <div class="emissionBasis-content-form">
            <normal-form-plus
              :formModel="formModels.formModel1"
              :config="configs.config1"
              :plus="plus"/>
          </div>
        </div>

        <!-- 基准排放量 -->
        <div class="emissionBasis-content">
          <div class="emissionBasis-content-title">基准排放量</div>
          <div class="emissionBasis-content-form">
            <normal-form-plus
              class="emissionBasis-content-form-wen"
              :formModel="formModels.formModel3"
              :config="configs.config3"
              :plus="plus1"/>
          </div>
        </div>

        <!-- 操作按钮 -->
        <div class="emissionBasis-btn">
          <el-button>取消</el-button>
          <el-button type="primary" @click="save">保存</el-button>
        </div>

      </div>

      <div slot="2" class="my-emissionBasis">
        <!-- 基本分类 -->
        <div class="emissionBasis-content">
          <div class="emissionBasis-content-title">基本分类</div>
          <div class="emissionBasis-content-form">
            <normal-form-plus
              :formModel="formModels.formModel1"
              :config="configs.config1"
              :plus="plus"/>
          </div>
        </div>

        <!-- 基准排放量 -->
        <div class="emissionBasis-content">
          <div class="emissionBasis-content-title">基准排放量</div>
          <div class="emissionBasis-content-form">
            <normal-form-plus
              class="emissionBasis-content-form-wen"
              :formModel="formModels.formModel3"
              :config="configs.config3"
              :plus="plus1"/>
          </div>
        </div>

        <!-- 操作按钮 -->
        <div class="emissionBasis-btn">
          <el-button>取消</el-button>
          <el-button type="primary" @click="save">保存</el-button>
        </div>

      </div>

      <div slot="3" class="my-emissionBasis">
        <!-- 基本分类 -->
        <div class="emissionBasis-content">
          <div class="emissionBasis-content-title">基本分类</div>
          <div class="emissionBasis-content-form">
            <normal-form-plus
              :formModel="formModels.formModel1"
              :config="configs.config1"
              :plus="plus"/>
          </div>
        </div>

        <!-- 基准排放量 -->
        <div class="emissionBasis-content">
          <div class="emissionBasis-content-title">基准排放量</div>
          <div class="emissionBasis-content-form">
            <normal-form-plus
              class="emissionBasis-content-form-wen"
              :formModel="formModels.formModel3"
              :config="configs.config3"
              :plus="plus1"/>
          </div>
        </div>

        <!-- 操作按钮 -->
        <div class="emissionBasis-btn">
          <el-button>取消</el-button>
          <el-button type="primary" @click="save">保存</el-button>
        </div>

      </div>
    </tab-helper>
  </div>


</template>

<script>
import TabHelper from '@/components/TabHelper'
import NormalFormPlus from '@/components/NormalFormPlus'

export default {
  components: {
    NormalFormPlus,
    TabHelper
  },
  mounted() {
    this.formModels.formModel3.factor[0].sum = 0
    if (this.$MOCK.mock) {
      this.formModels.formModel1 = {
        name1: "年度排放基准",
        name2: "2022",
        name3: "单一年份",
        name4: "企业的基准线排放量重算政策信息",
      }
    }
  },
  data() {
    return {
      nowYear: '',
      // 存储当前页面是有有数据，选择添加或更新
      paneStatusList: [false, false, false],
      paneIdList: {
        yearId: '',
        jiId: '',
        yueId: ''
      },
      paneType: 1,
      // 存储页面所有的节点数据，更新panel时进行切换
      factorList: [[], [], []],
      paneList: [
        {
          id: '1',
          label: '年度排放基准'
        },
        {
          id: '2',
          label: '季度排放基准'
        },
        {
          id: '3',
          label: '月度排放基准'
        },
      ],
      plus1: {
        edit: true,
        tableLabel: [
          {
            prop: "order1",
            label: "时间区间",
            edit: false
          },
          {
            prop: "sum",
            label: "总和",
            edit: false
          },
          {
            prop: "order2",
            label: "CHG(范围一)",
            edit: true
          },
          {
            prop: "order3",
            label: "CHG(范围二)",
            edit: true
          },
          {
            prop: "order4",
            label: "CHG(范围三)",
          },
          {
            prop: "order5",
            label: "ISO(直接排放或清除)",
          },
          {
            prop: "order7",
            label: "ISO(能源间接排放)",
          },
          {
            prop: "order8",
            label: "ISO(运输间接排放)",
          },
          {
            prop: "order9",
            label: "ISO(外购产品或服务间接排放)",
          },
          {
            prop: "order10",
            label: "IOS(供应链下游间接排放)",
          },
          {
            prop: "order11",
            label: "IOS(其他间接排放)",
          },


        ]
      },
      plus: {},
      formModels: {
        formModel1: {
          category: '',
          policy: '',
          order1: ''
        },
        formModel3: {
          factor: [
            {
              order1: '2023',
              sum: 0,
              order2: 0,
              order3: 0,
              order4: 0,
              order5: 0,
              order6: 0,
              order7: 0,
              order8: 0,
              order9: 0,
              order10: 0,
              order11: 0,

            },
          ]
        },
        formModel4: {},
      },
      configs: {
        config1: [
          {
            id: 2,
            items: [
              {
                id: '1',
                edit: true,
                type: 'select',
                span: 8,
                model: 'year',
                name: '基准年份',
                width: 150,
                props: {
                  placeholder: "基准年份",
                  options: [
                    {value: '2023', label: '2023'},
                  ]
                }
              },
            ]
          },
          {
            id: 3,
            items: [
              {
                id: '1',
                edit: true,
                type: 'text',
                span: 20,
                model: 'policy',
                name: '基准线排放量重算政策',
                width: 150,
                props: {
                  placeholder: "基准线排放量重算政策",
                  width: 200,
                  rows: 5
                }
              }
            ]
          }
        ],

        config3: [
          {
            id: 1,
            items: [
              {
                id: '1',
                edit: true,
                type: 'table',
                span: 8,
                model: 'factor',
                name: '排放因子',
                width: 60,
                props: {
                  placeholder: "排放因子类别",

                }
              }
            ]
          }
        ],

      }
    }
  },
  mounted() {
    this.getEmissionBasis();
  },
  methods: {
    // 获取tab index
    tabIndex(index) {
      var arr = [1, 2, 3]
      this.paneType = arr[index]
      // 更新表单数据
      this.formModels.formModel3.factor = this.factorList[arr[index] - 1];
    },
    // 获得排放基准
    getEmissionBasis() {
      this.factorList = [[], [], []],
        this.paneStatusList = [false, false, false],
        this.$API.enterprise.getEmissionBasis({}).then(response => {
          // 设置表单基础信息
          var year = '2023'
          // 处理排放基准
          var data = response.data
          for (var i = 0; i < data.length; i++) {
            if (!data[i].basisValueList) {
              continue;
            }
            if (data[i].category == 1) { // 年度标准
              year = response.data[i].year
              console.log('init year')
              console.log(year)
              this.nowYear = response.data[i].year + '-'
              this.formModels.formModel1.year = year
              this.formModels.formModel1.policy = response.data[i].policy
              this.paneIdList.yearId = response.data[i].id
              for (var j = 0; j < data[i].basisValueList.length; j++) {
                var numList = data[i].basisValueList[j].emissionClassification.split(",");
                var tem = {
                  order1: year,
                  sum: data[i].basisValueList[j].totalRelease,
                  order2: numList[0] ? numList[0] : 0,
                  order3: numList[1] ? numList[1] : 0,
                  order4: numList[2] ? numList[2] : 0,
                  order5: numList[3] ? numList[3] : 0,
                  order6: numList[4] ? numList[4] : 0,
                  order7: numList[5] ? numList[5] : 0,
                  order8: numList[6] ? numList[6] : 0,
                  order9: numList[7] ? numList[7] : 0,
                  order10: numList[8] ? numList[8] : 0,
                  order11: numList[9] ? numList[9] : 0,
                  id: data[i].basisValueList[j].id
                }
                this.factorList[0].push(tem)
              }
            } else if (data[i].category == 2) { // 季度标准
              this.paneIdList.jiId = response.data[i].id
              for (var j = 0; j < data[i].basisValueList.length; j++) {
                var numList = data[i].basisValueList[j].emissionClassification.split(",");
                var tem = {
                  order1: year + "-" + data[i].basisValueList[j].timeInterval,
                  sum: data[i].basisValueList[j].totalRelease,
                  order2: numList[0] ? numList[0] : 0,
                  order3: numList[1] ? numList[1] : 0,
                  order4: numList[2] ? numList[2] : 0,
                  order5: numList[3] ? numList[3] : 0,
                  order6: numList[4] ? numList[4] : 0,
                  order7: numList[5] ? numList[5] : 0,
                  order8: numList[6] ? numList[6] : 0,
                  order9: numList[7] ? numList[7] : 0,
                  order10: numList[8] ? numList[8] : 0,
                  order11: numList[9] ? numList[9] : 0,
                  id: data[i].basisValueList[j].id
                }
                this.factorList[1].push(tem)
              }
            } else if (data[i].category == 3) { // 月度标准
              this.paneIdList.yueId = response.data[i].id
              for (var j = 0; j < data[i].basisValueList.length; j++) {
                var numList = data[i].basisValueList[j].emissionClassification.split(",");
                var tem = {
                  order1: year + "-" + data[i].basisValueList[j].timeInterval,
                  sum: data[i].basisValueList[j].totalRelease,
                  order2: numList[0] ? numList[0] : 0,
                  order3: numList[1] ? numList[1] : 0,
                  order4: numList[2] ? numList[2] : 0,
                  order5: numList[3] ? numList[3] : 0,
                  order6: numList[4] ? numList[4] : 0,
                  order7: numList[5] ? numList[5] : 0,
                  order8: numList[6] ? numList[6] : 0,
                  order9: numList[7] ? numList[7] : 0,
                  order10: numList[8] ? numList[8] : 0,
                  order11: numList[9] ? numList[9] : 0,
                  id: data[i].basisValueList[j].id
                }
                this.factorList[2].push(tem)
              }
            }
          }
          // 更新表单状态，是否有数据
          for (var i = 0; i < 3; i++) {
            this.paneStatusList[i] = this.factorList[i].length > 0
          }
          if (this.factorList[1].length == 0) {
            for (var i = 0; i < 4; i++) {
              this.factorList[1].push({
                order1: year + "-" + (i + 1),
                sum: 0,
                order2: 0,
                order3: 0,
                order4: 0,
                order5: 0,
                order6: 0,
                order7: 0,
                order8: 0,
                order9: 0,
                order10: 0,
                order11: 0,
              })
            }
          }
          if (this.factorList[2].length == 0) {
            for (var i = 0; i < 12; i++) {
              this.factorList[2].push({
                order1: year + "-" + (i + 1),
                sum: 0,
                order2: 0,
                order3: 0,
                order4: 0,
                order5: 0,
                order6: 0,
                order7: 0,
                order8: 0,
                order9: 0,
                order10: 0,
                order11: 0,
              })
            }
          }
          this.formModels.formModel3.factor = this.factorList[0]
          console.log('deal list:')
          console.log(this.factorList)
        })
    },
    // 创建排放基准
    createEmissionBasis() {
      var basisValueList = []
      var li = this.formModels.formModel3.factor
      for (var i = 0; i < li.length; i++) {
        var arr = [li[i].order2, li[i].order3, li[i].order4, li[i].order5, li[i].order6, li[i].order7, li[i].order8, li[i].order9, li[i].order10, li[i].order11]
        var tem = {
          emissionClassification: arr.join(","),
          timeInterval: li[i].order1,
          totalRelease: li[i].sum
        }
        basisValueList.push(tem)
      }
      console.log(basisValueList)
      this.$API.enterprise.createEmissionBasis({
        year: this.formModels.formModel1.year,
        policy: this.formModels.formModel1.policy,
        category: this.paneType,
        basisValueList: basisValueList
      }).then(response => {
        this.$message({
          message: '创建成功',
          type: 'success'
        });
      })
    },
    // 更新排放基准
    updateEmissionBasis() {
      var basisValueList = []
      var li = this.formModels.formModel3.factor
      for (var i = 0; i < li.length; i++) {
        var arr = [li[i].order2, li[i].order3, li[i].order4, li[i].order5, li[i].order6, li[i].order7, li[i].order8, li[i].order9, li[i].order10, li[i].order11]
        var tem = {
          emissionClassification: arr.join(","),
          timeInterval: li[i].order1.replace(this.nowYear, ''),
          totalRelease: li[i].sum,
          id: li[i].id
        }
        basisValueList.push(tem)
      }
      var id = 0
      if (this.paneType == 1) {
        id = this.paneIdList.yearId
      } else if (this.paneType == 2) {
        id = this.paneIdList.jiId
      } else if (this.paneType == 3) {
        id = this.paneIdList.yueId
      }
      this.$API.enterprise.updateEmissionBasis({
        id: id,
        year: this.formModels.formModel1.year,
        policy: this.formModels.formModel1.policy,
        category: this.paneType,
        basisValueList: basisValueList
      }).then(response => {
        this.$message({
          message: '更新成功',
          type: 'success'
        });
      })
      console.log(
        JSON.stringify({
          id: id,
          year: this.formModels.formModel1.year,
          policy: this.formModels.formModel1.policy,
          category: this.paneType,
          basisValueList: basisValueList
        })
      )
    },
    // 保存排放标准
    save() {
      // 根据具体的情况 选择 更新或者创建
      if (this.paneStatusList[this.paneType - 1]) {
        console.log("update")
        this.updateEmissionBasis()
      } else {
        console.log("create")
        this.createEmissionBasis()
      }
    }
  },
  watch: {
    formModels: {
      deep: true,
      handler(newValue, oldValue) {
        for (var i = 0; i < this.formModels.formModel3.factor.length; i++) {
          this.formModels.formModel3.factor[i].sum = (parseInt(this.formModels.formModel3.factor[i].order2) ? parseInt(this.formModels.formModel3.factor[i].order2) : 0)
            + (parseInt(this.formModels.formModel3.factor[i].order3) ? parseInt(this.formModels.formModel3.factor[i].order3) : 0) + (parseInt(this.formModels.formModel3.factor[i].order4) ? parseInt(this.formModels.formModel3.factor[i].order4) : 0)
          // + (parseInt(this.formModels.formModel3.factor[i].order5)?parseInt(this.formModels.formModel3.factor[i].order5):0) + (parseInt(this.formModels.formModel3.factor[i].order6)?parseInt(this.formModels.formModel3.factor[i].order6):0)
          // + (parseInt(this.formModels.formModel3.factor[i].order7)?parseInt(this.formModels.formModel3.factor[i].order7):0) + (parseInt(this.formModels.formModel3.factor[i].order8)?parseInt(this.formModels.formModel3.factor[i].order8):0)
          // + (parseInt(this.formModels.formModel3.factor[i].order9)?parseInt(this.formModels.formModel3.factor[i].order9):0) + (parseInt(this.formModels.formModel3.factor[i].order10)?parseInt(this.formModels.formModel3.factor[i].order10):0)
          // + (parseInt(this.formModels.formModel3.factor[i].order11)?parseInt(this.formModels.formModel3.factor[i].order11):0)
        }

      },
    }
  }
}
</script>

<style lang="less" scoped>
.my-emissionBasis {
  width: 1200px;
}

.emissionBasis-content {

  padding: 10px;
  margin: 10px 0px;

  .emissionBasis-content-title {
    font-size: 18px;
    font-weight: 800;
  }

  .emissionBasis-content-form {
    margin: 20px 0px;

    .emissionBasis-content-form-wen {
      margin-left: -0.5vw;
      width: 250vw;
    }

    .emissionBasis-content-form-btn {

    }
  }
}

.emissionBasis-btn {
  margin: 0 auto;
  text-align: center;
}
</style>
